// pages/mine/mine.js
const app = getApp();
Page({

  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '我的',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333;background-color: #f1f1f1;",

    navs:[
      {title: "首页",class:"", path:"/pages/index/index", icon: "", font: "fa fa-home"},
      {title: "社区",class:"", path:"", icon: "", font: "fa fa-comments-o"},
      // {title: "维修",class:"", path:"", icon: "", font: "fa fa-wrench"},
      {title: "我的",class:"co", path:"/pages/mine/mine", icon: "", font: "fa fa-user-circle"},
    ],
    text: "<p>010-67217622、010-67262545<br/></p>",
    userInfo: {
      nickName: "",
      addr: "address",
      avatar: ""
    },
    ready: false
  },
  onLoad: function (options) {
  },
  onReady: function () {
  },
  onShow: function () {
    let v = this;
    console.log('onShow');
    if (app.globalData.token){
      v.getUserInfo();
    } else {
      app.doLogin(v,v.getUserInfo);
    }
  },
  getUserProfile(){
    let v = this;
    let info = v.data.userInfo;
    console.log('getUserProfile',info);
    if(!info.nickName) {
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: function(res) {
          var userInfo = res.userInfo;
          for(let k in userInfo) { info[k] = info[k] || userInfo[k]; }
          if(userInfo.avatarUrl) info.avatar = userInfo.avatarUrl;
          v.setData({userInfo: info});
          console.log("wx.getUserProfile",userInfo);
          v.saveUserInfo();
        },
        fail(err){
          console.log(err);
          v.navTo("/pages/userinfo/userInfo");
        }
      })
    } else {
      v.navTo("/pages/userinfo/userInfo");
    }
  },
  saveUserInfo(){
    let v= this;
    let url = app.globalData.host + '/api/user/upinfo';
    let data = {token: app.globalData.token};
    let info = v.data.userInfo;
    if(info.nickName) data.nickName = info.nickName;
    if(info.avatar) data.avatarUrl = info.avatar;
    if(info.gender) data.gender = info.gender;
    wx.request({
      url: url, data: data,method: "POST",
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("saveUserInfo",r);
          // if(r.msg) wx.showToast({title: r.msg, icon:"none" });
          v.navTo("/pages/userinfo/userInfo");
        }
      }
    })
  },
  navTo(path) {
    let v = this;
    console.log("navTo",path);
    wx.navigateTo({url: path});
  },
  navToPayHis(){
    let v = this;
    wx.navigateTo({url: '/pages/history/history'});
  },
  navToTicketHis(){
    let v = this;
    wx.navigateTo({url: '/pages/ticket/ticket'});
  },
  navToBind(ev) {
    wx.navigateTo({url: '/pages/binds/binds'});
  },
  navToRepairHis(ev) {
    wx.navigateTo({url: '/pages/applyRepair/repairList'});
  },
  navToRentHis(ev) {
    wx.navigateTo({url: '/pages/rent/order'});
  },
  getText(){
    let v = this;
    let url = app.globalData.host + '/api/common/text';
    wx.request({
      url: url, data: {id:1, token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let dd = r.data;
          let txt = dd[1];
          console.log('getText',txt,dd);
          v.setData({text: txt});
        }
      }
    })
  },
  getUserInfo(){
    let v = this;
    v.getText();
    let url = app.globalData.host + '/api/user/getinfo';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data.userInfo;
          if(l) {
            let info = v.data.userInfo;
            for(let k in l) info[k] = l[k];
            if(l.nickname) info.nickName = l.nickname;
            console.log("getUserInfo",info);
            v.setData({userInfo: info});
          }
        }
      }
    })
  },
  navTap(ev){
    let v = this;
    let ds = ev.currentTarget.dataset;
    let ll = v.data.navs;
    if(ds && ll[ds.idx]) {
      let lk = ll[ds.idx];
      let path = '/';
      if(lk){
        if(lk.path) {
          path = lk.path ;
          wx.reLaunch({ url: path});
        } else {
          wx.showToast({ title: '敬请期待', icon: 'none' })
        }
      }
      console.log('navTap',path, lk);
    }
  },
  
  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },
  onShareAppMessage: function () {
  }
})